package com.ruoyi.app.service.impl

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Fail
import com.ruoyi.app.common.Ok
import com.ruoyi.app.domain.vo.PicProcessResultVo
import com.ruoyi.app.service.Pic2PicModel
import com.ruoyi.app.service.Text2PicModel
import com.ruoyi.app.service.UserExperienceRecordApiService
import com.ruoyi.back.domain.v3.ExperienceRecord
import com.ruoyi.back.service.v3.ExperienceRecordService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class UserExperienceRecordApiServiceImpl : UserExperienceRecordApiService {

    @Autowired
    private lateinit var experienceRecordApiService: ExperienceRecordService

    override fun experienceRecord(id: Long): Data<PicProcessResultVo> =
        experienceRecordApiService.getById(id)?.let {
            Ok("生成成功", PicProcessResultVo().apply {
                resultImage = listOf(
                    it.resultPic!!
                )
                recordId = id
                prompt = it.descText
            })
        } ?: Fail("不存在当前模板")


    override fun getText2PicModel(): Data<List<Text2PicModel>> =
        experienceRecordApiService.list(KtQueryWrapper(ExperienceRecord::class.java)
            .eq(ExperienceRecord::type, 1)
            .orderByDesc(ExperienceRecord::createTime))
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取成功", map {
                    Text2PicModel().apply {
                        BeanUtil.copyProperties(it, this)
                    }
                })
            }
            ?: run {
                Ok("暂无数据", null)
            }

    override fun getPic2PicModel(): Data<List<Pic2PicModel>> =
        experienceRecordApiService.list(KtQueryWrapper(ExperienceRecord::class.java)
            .eq(ExperienceRecord::type, 2)
            .orderByDesc(ExperienceRecord::createTime))
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取成功", map {
                    Pic2PicModel().apply {
                        BeanUtil.copyProperties(it, this)
                    }
                })
            }
            ?: run {
                Ok("暂无数据", null)
            }


}